from elasticsearch import Elasticsearch


class ES:
    def __init__(self,url,Username,Password):
        self.es = Elasticsearch(
            hosts=[url],
            # http_auth=('your_username', 'your_password'),
            # basic_auth=('appuser', 'B4eE7Az503q8Fk6yg6sL'),
            http_auth=(Username, Password),
            headers={'Content-Type': 'application/json'}
        )

    def get_all_data(self,index_name,query):
        scroll_id = None
        data_list=[]
        page_size = 500  # 每页数据的大小
        while True:
            # 初始查询或下一页查询
            if scroll_id:
                response = self.es.scroll(scroll_id=scroll_id, scroll='1m')
            else:
                response = self.es.search(index=index_name, scroll='1m', size=page_size, body=query)
            # 处理当前页的数据
            hits = response['hits']['hits']
            data_list += list(map(lambda x: x['_source'], hits))
            scroll_id = response['_scroll_id']
            # 检查是否达到最后一页
            if len(hits) < page_size:
                break
        # 清除scroll上下文
        self.es.clear_scroll(scroll_id=scroll_id)
        print(f"获取ES的数目:{len(data_list)}")
        return data_list


if __name__ == '__main__':
    es=ES('http://139.159.245.112:9202','appuser','B4eE7Az503q8Fk6yg6sL')
    # filters = {
    #     "$and": [
    #         {"_id": {"$gt": ObjectId("62b427b0d405fbd40a496b43")}},
    #         {"_id": {"$lte": ObjectId("62b427b0d405fbd40a496ba3")}},
    #         {"Status": "Active"}
    #     ]
    #     }
    query ={
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "Brand":"MİS"
          }
        }
      ]
    }
  }
}
    es_brands = es.get_all_data('sensitive_wipo_distinctbrand_v2', query)
    print(es_brands)

